package org.yxt.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yxt.springcloud.entities.CommonResult;
import org.yxt.springcloud.entities.Payment;
import org.yxt.springcloud.service.PaymentService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping(value = "payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    public int port;

    @PostMapping(value = "create")
    public CommonResult creat(@RequestBody Payment payment) {
        int dbId = paymentService.save(payment);
        log.info("插入成功 {}", dbId);
        return new CommonResult(200, "success port: ".concat(String.valueOf(port)));
    }

    @GetMapping(value = "get/{id}")
    public CommonResult id(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        return new CommonResult(200, "success port: ".concat(String.valueOf(port)), paymentById);
    }

    /**
     * 获取注册在eureka中的服务列表信息
     *
     * @return
     */
    @GetMapping(value = "discovery")
    public Object discovery() {
        List<String> discoveryClientServices = discoveryClient.getServices();
        for (String element : discoveryClientServices) {
            log.info("当前注册的微服务id {}", element);
        }
        // 可以通过微服务的名称获取到微服务实例的详细信息 实例的application 名称
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance serviceInstance : instances) {
            log.info("当前微服务 CLOUD-PAYMENT-SERVICE 微服务名称 {} 主机 {} 端口 {} 地址 {}", new Object[]{serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort(), serviceInstance.getUri()});
        }
        return this.discoveryClient;
    }

    /**
     * 使用openFeign客户端请求数据时候，默认1秒必须响应数据，超过1秒中，自动报错 read time out
     * 因为业务处理不一定都在1秒内，所以自行设定OpenFeign客户端的超时时间
     *
     * @return
     */
    @GetMapping(value = "feign/timeout")
    public CommonResult feignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new CommonResult(1, "设置OpenFeign超时", String.valueOf(port));
    }

    @GetMapping(value = "lb")
    public CommonResult lb() {
        return new CommonResult(200, "success port: ".concat(String.valueOf(port)));
    }
}
